<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>表单页面</title>
    <link href="/static/pengpai/component/pear/css/pear.css" rel="stylesheet">
</head>
<body class="pear-container">
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">旧的密码:</label>
        <div class="layui-input-inline">
            <input type="password" name="old_password" required  lay-verify="required" placeholder="请输入密码框内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">初始密码:</label>
        <div class="layui-input-inline">
            <input type="password" name="password" required  lay-verify="required" placeholder="请输入密码框内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">验证密码:</label>
        <div class="layui-input-inline">
            <input type="password" required  lay-verify="required|confirmPass" placeholder="请输入密码框内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="user-updataPW">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="/static/pengpai/component/layui/layui.js"></script>
<script src="/static/pengpai/component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery','popup'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;

        form.on('submit(user-updataPW)', function (data) {
            let roleIds = "";
            $('input[type=checkbox]:checked').each(function () {
                roleIds += $(this).val() + ",";
            });
            roleIds = roleIds.substr(0, roleIds.length - 1);
            data.field.roleIds = roleIds;

            $.ajax({
                url: '/sysUser/updatePassword',
                data: {"old_password":data.field.old_password,"new_password":data.field.password},
                dataType: 'json',
                type: 'post',
                success: function (result) {
                    if (result.code === 200) {
                        popup.success(result.msg, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        popup.failure(result.msg);
                    }
                }
            })
            return false;
        });
        // 校验两次密码是否一致
        form.verify({
            confirmPass:function(value){
                if($('input[name=password]').val() !== value)
                    return '两次密码输入不一致！';
            }
        });
    })
</script>

</body>
</html>
